using System;
using System.Collections.Specialized;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MetaBuilders.Irc;
using MetaBuilders.Irc.Messages;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace MetaBuilders.NeboTests
{
	[TestClass]
	public class DebugFileTests : MessageTester
	{

		[TestMethod]
		public void DebugFileTest()
		{
			String logFileName = "";
			String[] resourceNames = this.GetType().Assembly.GetManifestResourceNames();
			foreach ( String resourceName in resourceNames )
			{
				if ( resourceName.Contains( "irclog.txt" ) )
				{
					logFileName = resourceName;
					break;
				}
			}

			Assert.IsFalse( String.IsNullOrEmpty( logFileName ) );

			Stream logStream = this.GetType().Assembly.GetManifestResourceStream( logFileName );
			using ( TextReader reader = new StreamReader( logStream ) )
			{
				while ( reader.Peek() >= 0 )
				{
					String line = reader.ReadLine().Trim();
					if ( line.Trim().Length == 0 )
					{
						continue;
					}

					if ( line.StartsWith( "-> " ) || line.StartsWith( "<- " ) )
					{
						line = ( ( line.StartsWith( "-> " ) ) ? ":" : "" ) + line.Substring( 3 );
					}

					IrcMessage msg = MessageParserService.Service.Parse( line );
					Type msgType = msg.GetType();
					Assert.IsNotInstanceOfType( msg, typeof( GenericErrorMessage ), "Message '{0}' parsed to generic message type '{1}'", line, msgType.Name );
					Assert.IsNotInstanceOfType( msg, typeof( GenericMessage ), "Message '{0}' parsed to generic message type '{1}'", line, msgType.Name );
					Assert.IsNotInstanceOfType( msg, typeof( GenericNumericMessage ), "Message '{0}' parsed to generic message type '{1}'", line, msgType.Name );
					Assert.IsNotInstanceOfType( msg, typeof( GenericCtcpReplyMessage ), "Message '{0}' parsed to generic message type '{1}'", line, msgType.Name );
					if ( msg is GenericCtcpRequestMessage )
					{
						GenericCtcpRequestMessage ctcp = msg as GenericCtcpRequestMessage;
						if ( ctcp.Command != "SLOTS" && ctcp.Command != "MP3" )
						{
							Assert.Fail( "Message '{0}' parsed to GenericCtcpRequestMessage", line );
						}
					}
				}
			}
			
		}

	}
}
